/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.boot.mybatis;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.iwindplus.boot.mybatis.domain.property.MybatisProperty;
import com.iwindplus.boot.mybatis.interceptor.MybatisOutputInterceptor;
import com.iwindplus.boot.mybatis.interceptor.MybatisInputInterceptor;
import com.iwindplus.boot.mybatis.manager.MybatisFieldManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * mybatis 字段配置管理.
 *
 * @author zengdegui
 * @since 2018/9/1
 */
@Slf4j
@AutoConfiguration(after = MybatisPlusAutoConfiguration.class)
@EnableConfigurationProperties({MybatisProperty.class, MybatisPlusProperties.class})
@ConditionalOnClass(MybatisPlusAutoConfiguration.class)
@ConditionalOnProperty(prefix = "mybatis-plus.field.crypto", name = "enabled", havingValue = "true")
public class MybatisFieldConfiguration {

    /**
     * mybatis字段管理器.
     *
     * @param mybatisPlusProperties mybatisPlusProperties
     * @param mybatisProperty       mybatisProperty
     * @return MybatisFieldManager
     */
    @Bean
    public MybatisFieldManager mybatisFieldManager(MybatisPlusProperties mybatisPlusProperties, MybatisProperty mybatisProperty) {
        final MybatisFieldManager mybatisFieldManager = new MybatisFieldManager(mybatisPlusProperties, mybatisProperty);
        log.info("MybatisFieldManager={}", mybatisFieldManager);
        return mybatisFieldManager;
    }

    /**
     * 入参拦截器（加密/解密/脱敏）.
     *
     * @return MybatisInputInterceptor
     */
    @Bean
    public MybatisInputInterceptor mybatisInputInterceptor() {
        final MybatisInputInterceptor mybatisInputInterceptor = new MybatisInputInterceptor();
        log.info("MybatisInputInterceptor={}", mybatisInputInterceptor);
        return mybatisInputInterceptor;
    }

    /**
     * 出参拦截器（加密/解密/脱敏）.
     *
     * @return MybatisOutputInterceptor
     */
    @Bean
    public MybatisOutputInterceptor mybatisOutputInterceptor() {
        final MybatisOutputInterceptor mybatisOutputInterceptor = new MybatisOutputInterceptor();
        log.info("MybatisOutputInterceptor={}", mybatisOutputInterceptor);
        return mybatisOutputInterceptor;
    }
}
